package com.starsoft.frame.base.service;

import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.starsoft.frame.base.bean.BaseAttachment;
import com.starsoft.frame.base.dao.AttachmentDao;

@Service
public class AttachmentService {
	
	@Autowired
	AttachmentDao attachmentDao;
	
	public BaseAttachment getFile(Integer id){
		return attachmentDao.get(id);
	}
	
	public void delete(Integer id){
		BaseAttachment file = getFile(id);
		attachmentDao.delete(file);
	}
	
	public String getFileName(Integer id ){
		return attachmentDao.getFileName(id);
	}
	
	public Integer saveAttachment(String fileName, InputStream in){
		return attachmentDao.saveAttachment(fileName, in);
	}
	
	public void write(OutputStream stream, Blob content){
		try {
			 InputStream in=content.getBinaryStream();
		     byte[] b =new byte[in.available()];
		     in.read(b);
			 stream.write(b);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
